//
//  MHComicCommentTableViewCell.m
//  ManHua
//
//  Created by 江子安 on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHComicCommentTableViewCell.h"
#import <UIButton+WebCache.h>
#import "UIImage+Addiction.h"
#import <SDWebImageManager.h>

@implementation MHComicCommentTableViewCell





- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _photoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _photoBtn.frame = CGRectMake(10, 10, 40, 40);
        [_photoBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-ren"] forState:UIControlStateNormal];
        [self.contentView addSubview:_photoBtn];
        
        _nickLabel = [[UILabel alloc]init];
        _nickLabel.frame = CGRectMake(60, 10, 200, 25);
        _nickLabel.textAlignment = NSTextAlignmentLeft;
       
        [self.contentView addSubview:_nickLabel];
        
        _TypeNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 35, 200, 15)];
        _TypeNameLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_TypeNameLabel];
        
        _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 120, 10, 110, 20)];
        _dateLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_dateLabel];
        
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 60, SCREEN_WIDTH - 80, 40)];
        _contentLabel.numberOfLines = 0;
//        _contentLabel.backgroundColor = [UIColor greenColor];
        _contentLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_contentLabel];
        
        
        float YY =  _contentLabel.frame.size.height + _contentLabel.frame.origin.y + 10;
        _likeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _likeBtn.frame = CGRectMake(60,YY, 20, 20);
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-zan"] forState:UIControlStateNormal];
        [self.contentView addSubview:_likeBtn];
        
        _likeCount = [[UILabel alloc]initWithFrame:CGRectMake(80, YY, 30, 20)];
        _likeCount.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_likeCount];
        
        _replyImg = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 80, YY, 20, 20)];
        _replyImg.image = [UIImage imageNamed:@"iconfont-pinglun"];
        [self.contentView addSubview:_replyImg];
        
        _replyCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 60 , YY, 40, 20)];
        _replyCountLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_replyCountLabel];
        
        
        
    }
    return self;
}

+ (CGFloat)heightOfString:(NSString *)string
{
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 80, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightOfCellWithModdel:(MHComicComentModel *)model
{
    if ([self heightOfString:model.CommentContent] > 40) {
        return 80 + [self heightOfString:model.CommentContent]+20;

    }
    else
    {
        return 130;
    }
}


// 加载数据
- (void)configureDataWithModel:(MHComicComentModel *)model
{
    if (model.UserPhotoURL.length >1) {
        SDWebImageManager *manager = [[SDWebImageManager alloc]init];
        [manager downloadImageWithURL:[NSURL URLWithString:model.UserPhotoURL] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            [_photoBtn setBackgroundImage: [UIImage circleImageWithImage:image borderWidth:0 borderColor:[UIColor clearColor]] forState:UIControlStateNormal];
        }];
    }
    _nickLabel.text = model.UserNick;

    _TypeNameLabel.text = model.ClientTypeName;
    _dateLabel.text = model.CommentDate;
 
    _contentLabel.text = model.CommentContent;
    
    _likeCount.text = [NSString stringWithFormat:@"%@",model.LikeCount];
    _replyCountLabel.text = [NSString stringWithFormat:@"%@",model.ReplyCount];
    
    CGFloat height = [MHComicCommentTableViewCell heightOfString:model.CommentContent];
    [self setFrameWithHeight:height];
    
}

- (void)setFrameWithHeight:(CGFloat)height
{
    _contentLabel.frame = CGRectMake(60, 60, SCREEN_WIDTH - 80, height);
    
    float YY =  _contentLabel.frame.size.height + _contentLabel.frame.origin.y + 10;
    
    _likeBtn.frame = CGRectMake(60,YY, 20, 20);
    _likeCount.frame = CGRectMake(80, YY, 30, 20);
    _replyImg.frame = CGRectMake(SCREEN_WIDTH - 80, YY, 20, 20);
    _replyCountLabel.frame = CGRectMake(SCREEN_WIDTH - 60 , YY, 40, 20);
  
 

}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
